from random import random

import numpy as np

from SAGIN_Progrem.ENV import Env

def run(e, multi_action):
    off_sa = [[], [], [], [], []]
    off_sa_list = [[], [], [], [], []]
    sa_com = []
    # 卫星要本地计算的任务：无人机卸载的+收集的
    for task in e.sate[0].collect(e.task_list_passion_sa[e.now_step], e.step_time):
        sa_com.append(task)
    for i in range(0, e.uav_num):
        arrive_time_list = e.uav_list[i].collect_c(e.task_list_passion_uav[i][e.now_step], e.step_time)
        # 在计算队列中计算要卸载的任务
        off_sa_temp = e.uav_list[i].compute_task_n2_(arrive_time_list, e.task_list_passion_uav[i][e.now_step],
                                                        e.step_time, multi_action[i], e.now_step + 1)
        # 获取所有卸载到卫星的任务
        off_sa[i].append(off_sa_temp)
        # 获取能传输到卫星的任务
        off_sa_list_temp = e.uav_list[i].off_to_sate_c(off_sa[i][0], e.step_time)  # 卸载到卫星的
        # 统计
        off_sa_list[i].append(off_sa_list_temp)
        for task in off_sa_list[i][0]:
            sa_com.append(task)
    # 卫星计算
    e.sa_com = sa_com
    off_cloud_temp = e.sate[0].compute_task_(sa_com, e.step_time, multi_action[e.uav_num],
                                             e.now_step + 1)
    off_cloud = e.sate[0].off_to_cloud(off_cloud_temp, e.step_time)
    # 云计算
    e.cloud[0].compute_task(off_cloud, e.step_time, e.now_step + 1)
env = Env(N=6, max_cycles=25)
env.init_entity()
def get_actions():
    actions = []  # 卸载矩阵  是6*40
    for i in range(0, 5):
        action_ = []  # 卸载矩阵的一行
        for j in range(0, 2):
            action_.append(random())
        actions.append(action_)
    return actions
for i in range(25):
    env.now_step = i
    run(env, [0, 0, 0, 0, 0, 0])

local_total_finish = []
local_sa, local_sa_, local_uav, local_uav_ = 0, 0, 0, 0
for i in range(25):
    for task in env.task_list_passion_sa[i]:
        local_sa_ += 1
        if not task.finish == 0:
            local_sa += 1
            local_total_finish.append(task)
    for j in range(env.agents_num - 1):
        for task in env.task_list_passion_uav[j][i]:
            local_uav_ += 1
            if not task.finish == 0:
                local_uav += 1
                local_total_finish.append(task)
local_finish_rate = (local_sa + local_uav) / (local_sa_ + local_uav_)
local_delay_list = []
for task in local_total_finish:
    delay = task.delay['wait'] + task.delay['compute'] + task.delay['transmission']
    local_delay_list.append(delay)



